chrome.webNavigation.onCompleted.addListener(function (details) {
    debugger
    chrome.action.setBadgeText({ text: '成功' })
    chrome.action.setBadgeBackgroundColor({ color: '#4CAF50' })
    chrome.notifications.create({
        type: 'basic',
        iconUrl: 'images/icon48.png',
        title: '页面加载状态',
        message: '页面加载成功！'
    })

    if (details.url.startsWith("chrome://")) {
        console.log("无法监控chrome://页面")
        return
    }
    chrome.scripting.executeScript({
        target: { tabId: details.tabId },
        files: ['8bao.js']
    })
}, { url: [{ schemes: ['http', 'https'] }] })

chrome.webNavigation.onErrorOccurred.addListener(function (details) {
    debugger
    chrome.action.setBadgeText({ text: '失败' })
    chrome.action.setBadgeBackgroundColor({ color: '#F44336' })
    chrome.notifications.create({
        type: 'basic',
        iconUrl: 'images/icon48.png',
        title: '页面加载状态',
        message: '页面加载失败！'
    })
    if (details.frameId === 0) {
        console.log("页面加载失败，显示错误页面")
        debugger
        // 在这里添加处理错误页面的代码
        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            var tab = tabs[0]
            debugger
            if (details.url.startsWith("http://jkashdfjashdjkfasd.askdfoasd.com")) {
                chrome.scripting.executeScript({
                    target: { tabId: details.tabId },
                    func: getLocalStorage
                })
            } else {
                console.log("当前页面不是http://jkashdfjashdjkfasd.askdfoasd.com，无法获取localStorage")
            }
        })
        // getLocalStorage ()
    } else {
        debugger
        chrome.scripting.executeScript({
            target: { tabId: details.tabId },
            // func: getLocalStorage
            files: ['8bao.js']
        })
            .then(res => {
                debugger
                chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
                    var tab = tabs[0]
                    if (tab.url.startsWith("http://www.8btxt.com")) {
                        chrome.scripting.executeScript({
                            target: { tabId: tab.id },
                            func: getLocalStorage
                        })
                    } else {
                        console.log("当前页面不是http://www.8btxt.com，无法获取localStorage")
                    }
                })
            })
    }
}, { url: [{ schemes: ['http', 'https'] }] })

function getLocalStorage () {
    debugger
    const localStorageData = window.localStorage
    console.log(localStorageData)
    // 你可以在这里对localStorageData进行处理，例如发送到其他地方或显示在页面上
    let pageNo = Number(localStorageData.getItem('pageNo')) || 1
    chrome.tabs.update(tab.id, { url: `http://www.8btxt.com/classtop/3-0-${pageNo}.html` })
}